package com.shy.springframework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import com.shy.springframework.beans.factory.config.BeanDefinition;
import com.shy.springframework.stereotype.Component;
import com.shy.springframework.util.ClassUtils;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;

/**
 * @author yzpang
 * @desc: 扫描指定包名下的拥有指定注解的类
 * @date: 2023/2/13 13:11
 */
public class ClassPathScanningCandidateComponentProvider {

    /**
     * 扫描指定包下有Component注解的类,
     * 生成BeanDefinition集合
     * @param basePackage
     * @return
     */
    public Set<BeanDefinition> findCandidateComponents(String basePackage){
        LinkedHashSet<BeanDefinition> candidate = new LinkedHashSet<>();
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Component.class);
        for (Class<?> clazz : classes) {
            BeanDefinition beanDefinition = new BeanDefinition(clazz);
            candidate.add(beanDefinition);
        }
        return candidate;
    }
}
